package com.aierjun.test;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.aierjun.test.APIManager.API;
import com.aierjun.test.utils.ClearCacheUtils;
import com.aierjun.test.utils.LogUtils;
import com.aierjun.test.utils.SettingsWebViewUtils;
import com.aierjun.test.utils.SharedPreferencesUtils;

import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaChromeClient;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by aierJun on 2016/12/22.
 */
public class BaseMainActivity extends CordovaActivity {
    private CordovaWebView webView;
    private ImageView imageView;
    private FrameLayout root;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_webview);
        findView();
        initView();
    }

    private void findView() {
        webView= (CordovaWebView) findViewById(R.id.one_webview_view);
        imageView= (ImageView) findViewById(R.id.one_webview_imageview);
        root= (FrameLayout) findViewById(R.id.root);
    }

    private void initView() {
        loadUrl("http://www.woxiangwoxiang.com:8013");
    }

    @Override
    public void init() {
        this.init(webView, new CordovaWebViewClient(this, webView) {
            // 页面加载完成事件
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view,url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        }, new CordovaChromeClient(this, webView) {
            // 页面加载中的事件
            @Override
            public void onProgressChanged(final WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });
        };



    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else {
            exit();
        }
    }

    @Override
    public void onDestroy() {
        ClearCacheUtils.cleanDatabases(BaseMainActivity.this);
        finish();
        super.onDestroy();
    }

    // 定义一个变量，来标识是否退出
    private static boolean isExit = false;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), R.string.exit_again,
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, API.HANDLER_MAIN);
        } else {
            super.onBackPressed();
            ClearCacheUtils.cleanDatabases(BaseMainActivity.this);   //暂时可以清楚缓存，当涉及到保存用户账户密码时一定要关闭
            finish();
        }
    }
}
